using UnityEngine;

namespace GGameFramework.Core.Pool
{
    public class DynamicPoolingStrategy : IPoolingStrategy
    {
        public int MaxSize
        {
            get { return _maxSize; }
        }

        // 最大数量
        private readonly int _maxSize;
        
        //收缩间隔
        private readonly float _shrinkInterval;
        
        //上次收缩时间
        private float _lastShrinkTime;

        public DynamicPoolingStrategy(int maxSize = 100, float shrinkInterval = 30f)
        {
            _maxSize = maxSize;
            _shrinkInterval = shrinkInterval;
        }

        public void HandlePoolSize(IObjectPool pool)
        {
            if (pool.Count <= _maxSize) return;
            
            // 检查是否需要缩减池大小
            if (Time.time - _lastShrinkTime > _shrinkInterval && pool.Count > pool.Capacity)
            {
                int toRemove = pool.Count - pool.Capacity;
                for (int i = 0; i < toRemove; i++)
                {
                    // 实际缩减操作由对象池实现
                    
                }

                _lastShrinkTime = Time.time;
            }
        }
        
        public bool ShouldDestroyObject(IObjectPool pool, int currentCount)
        {
            return currentCount >= _maxSize;
        }
    }
}